<?php
	/* Session and DB connection */
	session_start();

	$link = mysql_connect('localhost', 'root', '');
	mysql_select_db("gameoflife");
	if (!$link) {
		die('Could not connect: ' . mysql_error());
	}
	
	/* Functions */
	
	function current_date_time() {
		return date("Y-m-d H:i:s");
	}
	
	function format_date_time($datetime) {
		$date = new DateTime($datetime);
		return $date->format("d M Y g:ia");
	}
	
	function debug($obj) {
		echo "<pre>";
		print_r($obj);
		echo "</pre>";
	}
	
	function format_money($amount) {
		return ($amount < 0 ? "<font class=\"money-negative\">-" : "") . 
			"$" . number_format(abs($amount)) . ($amount < 0 ? "</font>" : "");
	}
	
	function get_settings() {
		return mysql_fetch_assoc(mysql_query("SELECT * FROM `settings` LIMIT 1"));
	}
	
	function get_millionaire_lifecards($gameid) {
		$strIds = mysql_result(mysql_query("SELECT `millionairelifecards` FROM `game` WHERE `id` = '$gameid' LIMIT 1"), 0);
		$aryIds = explode("|", $strIds);
		$aryIdsFiltered = array();
		foreach ($aryIds as $id) {
			if ($id != "") {
				array_push($aryIdsFiltered, $id);
			}
		}
		return $aryIdsFiltered;
	}
	
	function get_millionaire_lifecards_query_condition($gameid) {
		$ary = get_millionaire_lifecards($gameid);
		return "(`lifecard`.`id` = '" . implode("' OR `lifecard`.`id` = '", $ary) . "')";
	}
	
	/**
		@param string $type The type of card - career, salary, house, life or stock
							This determines how the cards are rendered
		@param string $query_string The query string used to fetch the cards from the db
		@param string $player_steal_from The ID of the player you will steal from (only when lifecards run out)
	*/
	function cards_html($type, $query_string, $player_steal_from = "") {
		$html = "<table class=\"cards\" type=\"$type\"><tr>";
		$card_query = mysql_query($query_string);
		while ($card = mysql_fetch_assoc($card_query)) {
			$html .= 
				"<td cardId=\"" . $card['id'] . "\" class=\"card-type-$type\" stealFrom=\"$player_steal_from\">" .
					// Card back (image)
					"<img class=\"card-back\" src=\"images/card-backs/$type." . 
							($type == "life" ? "png" : "jpg") . "\"/>" .
					// Card front (div)
					"<div class=\"card-front\">" .
						"<div class=\"spacer\"></div>";
			// Template image for stocks
			if ($type == "stock") {
				$html .= "<img class=\"template\" src=\"images/stocks/template.png\"/>";
			}
			// Icon
			if ($type == "life") {
				$html .= "<img class=\"icon\" src=\"images/icons/unknown_32.png\" alt=\"???\"/>";
			} elseif ($type != "salary") {
				$html .=
						"<img class=\"icon\" src=\"images/${type}s/" . 
								strtolower(str_replace(" ", "-", 
									(isset($card['name']) ? $card['name'] : $card['id']))) . 
								".png\"/>";
			}
			// Description
			if ($type == "house") {
				$html.= "<div class=\"name\">" . $card['name'] . "</div>" .
						"<div class=\"description\">" . $card['description'] . "</div>" .
						"<div class=\"agent\">" . $card['agent'] . "</div>" .
						"<div class=\"price\">" . format_money($card['price']) . "</div>" .
						"<div class=\"insurance\">Insurance: " . format_money($card['insurance']) . "</div>";
			} elseif ($type == "salary") {
				$html .= "<div class=\"collect\">Collect</div>" .
						"<div class=\"income\">" . format_money($card['income']) . "</div>" .
						"<div class=\"collect\">on payday</div>" .
						"<div class=\"pay1\">Pay</div>" .
						"<div class=\"tax\">" . format_money($card['tax']) . "</div>" .
						"<div class=\"pay2\">on taxes due</div>";
			}
			$html .=
					"</div>" .
				"</td>";
		}
		$html .= "</tr></table>";
		return $html;
	}
	
	function lifecards_html($lifecards, $reveal = false) {
		$lifecards_html = "";
		if (!$reveal) {
			$lifecards_html .= "<div class=\"lifecard-stack\">";
			foreach ($lifecards as $lifecard) {
				$lifecards_html .= "<div class=\"lifecard-stack-item\">";
			}
			if (count($lifecards) > 0) {
				$lifecards_html .= count($lifecards);
			}
			foreach ($lifecards as $lifecard) {
				$lifecards_html .= "</div>";
			}
			$lifecards_html .= "</div>";
		} else {
			$lifecards_html .= "<div class=\"lifecard-layout\">";
			foreach ($lifecards as $lifecard) {
				$lifecards_html .= "
					<div class=\"lifecard-item\">
						<span class=\"achievement\">" . $lifecard['achievement'] . "</span>
						<span class=\"prize\">" . format_money($lifecard['prize']) . "</span>
					</div>";
			}
			$lifecards_html .= "</div>";
		}
		return $lifecards_html;
	}
	
	/* Global Variables */
	
	if (isset($_SESSION['game'])) {
		$current_game = mysql_fetch_assoc(mysql_query("SELECT * FROM `game` WHERE `id` = '" . $_SESSION['game'] . "'"));
		$current_game_id = $current_game['id'];
	}
?>
